#ifndef STATUS_SERVE_SERVER_H
#define STATUS_SERVE_SERVER_H
#include <grpc++/grpc++.h>
#include "../rpc_src/message.pb.h"
#include "../rpc_src/message.grpc.pb.h"
#include <list>
#include <atomic>
#include <unordered_map>
#include <shared_mutex>
#include <ctime>
#include <thread>

//检测间隔为180s
#define CHECK_HEART_THREAD_GAP 180   
struct chat_server_address{
    std::string host;
    std::string port;
    std::string user_set;   //该chat_server在redis中对应于用户列表的key
    std::string chat_server_name;   //该chat_server在redsi中对应的当前人数的chat_server的field
    std::string rpc_server_info;
    std::time_t last_heart_beat;

};


class statusServe_rpc:public message::statusServer::Service{

public:

    virtual ::grpc::Status get_chatServer_address(::grpc::ServerContext* context, const ::message::reqGetChatServerAddress* request, ::message::resGetChatServerAddress* response);
    virtual ::grpc::Status registerChat(::grpc::ServerContext* context, const ::message::reqRegisterChat* request, ::message::resRegisterChat* response);
    virtual ::grpc::Status checkToken(::grpc::ServerContext* context, const ::message::reqCheckToken* request, ::message::resCheckToken* response);
    virtual ::grpc::Status deleteChat(::grpc::ServerContext* context, const ::message::reqDeleteChat* request, ::message::resDeleteChat* response);
    virtual ::grpc::Status heartBeat(::grpc::ServerContext* context, const ::message::reqHeartBeat* request, ::message::resHeartBeat* response);
    virtual ::grpc::Status getForwardTarget(::grpc::ServerContext* context, const ::message::reqGetForwardTarget* request, ::message::resGetForwardTarget* response);

    
    statusServe_rpc();
    ~statusServe_rpc();
private:
   

    std::list<chat_server_address *> working_chat_server_list;   //由于数量少，所以直接采用list存放即可

    std::shared_mutex  s_mtx;   //采用共享锁
    chat_server_address *get_fit_chat_server();
    std::string get_uuid();
    // --email---token
    std::unordered_map<std::string,std::string>token_map;   //目前不知道该拿来怎么鉴权的
    void add_chat_server(chat_server_address *);
    void delete_chat_server(std::string host,std::string port);
    chat_server_address *check_host_port(const std::string host,const std::string port);
    std::thread check_heart_thread;
    
  


};



#endif